package com.personal.system.service.impl.content;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.personal.common.result.PageUtils;
import com.personal.common.result.Result;
import com.personal.system.entity.content.Tags;
import com.personal.system.entity.vo.SystemTagListVo;
import com.personal.system.exception.GlobalException;
import com.personal.system.mapper.content.TagsMapper;
import com.personal.system.service.content.TagsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TagsServiceImpl extends ServiceImpl<TagsMapper, Tags> implements TagsService {

    @Resource
    private TagsMapper tagsMapper;

    /**
     * 标签列表
     * @param name
     * @return
     */
    @Override
    public Result listTags(String name) {
        Page<SystemTagListVo> list = tagsMapper.selectPageRecord(new Page<>(PageUtils.getPageNo(), PageUtils.getPageSize()),name);
        return Result.ok(list);
    }

    /**
     * 标签详情
     * @param id
     * @return
     */
    @Override
    public Result getTagsById(Long id) {
        Tags tags = tagsMapper.selectById(id);
        return Result.ok(tags);
    }

    /**
     * 添加标签
     * @param tags
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result insertTag(Tags tags) {
        validateName(tags.getName());
        tagsMapper.insert(tags);
        return Result.ok();
    }

    /**
     * 修改标签
     * @param tags
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateTag(Tags tags) {
        Tags entity = tagsMapper.selectById(tags.getId());
        if (!entity.getName().equalsIgnoreCase(tags.getName())){
            validateName(tags.getName());
        }
        tagsMapper.updateById(tags);
        return Result.ok();
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteById(Long id) {
        tagsMapper.deleteById(id);
        return Result.ok();
    }

    /**
     * 批量删除标签
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteBatch(List<Long> ids) {
        tagsMapper.deleteBatchIds(ids);
        return Result.ok();
    }

    /**
     * 置顶标签
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result top(Long id) {
        Tags tags = tagsMapper.selectOne(new QueryWrapper<Tags>()
                .last("limit 1").orderByDesc("sort"));
        Assert.isTrue(!tags.getId().equals(id),"改标签已在最顶端!");
        Tags entity = Tags.builder().id(id).sort(tags.getSort()+1).build();
        int rows = tagsMapper.updateById(entity);
        return rows > 0 ? Result.ok(): Result.fail();
    }


    public void validateName(String name){
        Tags entity = tagsMapper.selectOne(new QueryWrapper<Tags>().eq("name",name));
        if (ObjectUtils.isNotEmpty(entity)) {
            throw new GlobalException(404,"标签名已存在");
        }
    }
}
